﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class TerrainElementEditor : Form
    {
        String bmpFileName;
        UInt16 CurrentType;
        String elemFileName;

        public static Bitmap bmpBlock = new Bitmap("Block.jpg");

        Point StartPos;

        CTerrainElement NewElement;
        bool bDrawBlock = false;
        bool bEraseBlock = false;


        public TerrainElementEditor()
        {
            InitializeComponent();
            Init();
        }

        void Init()
        {
            bmpFileName = null;
            CurrentType = 0;
            NewElement = new CTerrainElement(); ;
            elemFileName = "";
        }

        private void SelectTerrainPic_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "位图文件|*.bmp|JPEG文件|*.jpg";
            openFileDialog.InitialDirectory = CONST.ROOTPATH + "\\" +
                CONST.TERRAINPATH;

            if( openFileDialog.ShowDialog() == DialogResult.OK )
            {
                Bitmap bmpTemp = new Bitmap(openFileDialog.OpenFile());

                if (bmpTemp.Width % 16 != 0 || bmpTemp.Height % 16 != 0)
                {
                    MessageBox.Show(CONST.BMPDIVIDEBY16);
                    return;
                }

                if (bmpTemp.Width > 1000 || bmpTemp.Height > 600)
                {
                    MessageBox.Show(CONST.BMPTOOBIG);
                    return;
                }
                

                int headerHeight = this.Height - this.ClientSize.Height;

                this.Width = bmpTemp.Width + 20;
                this.Height = bmpTemp.Height + toolStrip1.Height + headerHeight + 20;

                if (this.Width < 500)
                {
                    this.Width = 500;
                }

                if (this.Height < 300)
                {
                    this.Height = 300;
                }
                Init();
                


                NewElement.bmpTerrainElement = bmpTemp;
                NewElement.bmpProperty = new Bitmap(NewElement.bmpTerrainElement.Width, NewElement.bmpTerrainElement.Height);
                Graphics g = Graphics.FromImage(NewElement.bmpProperty);
                g.Clear(Color.FromArgb(0, 0xFF, 0xFF, 0xFF));
                Pen pen = new Pen(Color.Black, 1);

                for (int i = 16; i <= NewElement.bmpProperty.Width; i += 16)
                {
                    g.DrawLine(pen, i, 0, i, NewElement.bmpProperty.Height);
                }
                for (int i = 16; i <= NewElement.bmpProperty.Height; i += 16)
                {
                    g.DrawLine(pen, 0, i, NewElement.bmpProperty.Width, i);
                }

                StartPos.X = (TerrainPic.Width - NewElement.bmpTerrainElement.Width) / 2;
                StartPos.Y = (TerrainPic.Height - NewElement.bmpTerrainElement.Height) / 2;

                TerrainPic.Invalidate();

                bmpFileName = openFileDialog.SafeFileName;
                

                NewElement.mBmpName = bmpFileName;
                NewElement.mBlocks = new CTerrainElement.Block[ NewElement.bmpProperty.Width /16 , NewElement.bmpProperty.Height /16 ];
            }
        }

        private void TerrainPic_Paint(object sender, PaintEventArgs e)
        {
            if (NewElement.bmpTerrainElement != null)
            {
                e.Graphics.DrawImage(NewElement.bmpTerrainElement, StartPos);
            }
            if (NewElement.bmpProperty != null)
            {
                e.Graphics.DrawImage(NewElement.bmpProperty, StartPos);

                for (int i = 0; i < NewElement.bmpProperty.Width / 16;i++ )
                {
                    for (int j = 0; j < NewElement.bmpProperty.Height / 16; j++ )
                    {
                        if (NewElement.mBlocks[i,j] == null)
                        {
                            continue;
                        }
                        if ( NewElement.mBlocks [i,j].BT != 0)
                        {
                            int tempX = StartPos.X + i * 16;
                            int tempY = StartPos.Y + j * 16;
                            e.Graphics.DrawImage(bmpBlock,tempX,tempY);
                        }
                    }
                }
            }
        }

        private void PaintBlock_Click(object sender, EventArgs e)
        {
            if (PaintBlock.CheckState == CheckState.Checked)
            {
                if (EraseBlock.CheckState == CheckState.Checked)
                {
                    EraseBlock.CheckState = CheckState.Unchecked;
                }
            }
        }

        private void TerrainPic_MouseMove(object sender, MouseEventArgs e)
        {
            if ((PaintBlock.CheckState == CheckState.Checked) && bDrawBlock)
            {
                int tempX = e.X - StartPos.X;
                int tempY = e.Y - StartPos.Y;

                if (tempX >= 0 && tempY >= 0 && tempX < NewElement.bmpProperty.Width && tempY < NewElement.bmpProperty.Height)
                {
                    int indexX = tempX / 16;
                    int indexY = tempY / 16;

                    CTerrainElement.Block b;
                    if (NewElement.mBlocks[indexX, indexY] == null)
                    {
                        b = new CTerrainElement.Block();
                        NewElement.mBlocks[indexX, indexY] = b;
                    }
                    NewElement.mBlocks[indexX, indexY].BT = CurrentType;
                    NewElement.mBlocks[indexX, indexY].Value = Convert.ToInt32(extValueEdit.Text);
                    TerrainPic.Invalidate();
                    return;
                }
            }

            if ((EraseBlock.CheckState == CheckState.Checked) && bEraseBlock)
            {
                int tempX = e.X - StartPos.X;
                int tempY = e.Y - StartPos.Y;

                if (tempX >= 0 && tempY >= 0 && tempX < NewElement.bmpProperty.Width && tempY < NewElement.bmpProperty.Height)
                {
                    int indexX = tempX / 16;
                    int indexY = tempY / 16;

                    NewElement.mBlocks[indexX, indexY] = null;
                    TerrainPic.Invalidate();
                    return;
                }
            }
        }
        
        private void TerrainPic_MouseDown(object sender, MouseEventArgs e)
        {
            if ((PaintBlock.CheckState == CheckState.Checked))
            {
                bDrawBlock = true;
            }

            if ((EraseBlock.CheckState == CheckState.Checked))
            {
                bEraseBlock = true;
            }
        }

        private void TerrainPic_MouseUp(object sender, MouseEventArgs e)
        {
            bDrawBlock = false;
            bEraseBlock = false;
        }

        private void EraseBlock_Click(object sender, EventArgs e)
        {
            if ( EraseBlock.CheckState == CheckState.Checked )
            {
                if (PaintBlock.CheckState == CheckState.Checked)
                {
                    PaintBlock.CheckState = CheckState.Unchecked;
                }
            }
        }
        private void NormalTerrain_Click(object sender, EventArgs e)
        {
            NormalTerrain.Checked = !NormalTerrain.Checked;
            if (NormalTerrain.Checked)
            {
                CurrentType |= CTerrainElement.NormalTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.NormalTerrain;
            }

            typelable.Text = CurrentType.ToString();
        }
        private void LeftSpeedTerrain_Click(object sender, EventArgs e)
        {
            LeftSpeedTerrain.Checked = !LeftSpeedTerrain.Checked;
            if (LeftSpeedTerrain.Checked)
            {
                CurrentType |= CTerrainElement.LeftSpeedTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.LeftSpeedTerrain;
            }
            typelable.Text = CurrentType.ToString();
        }

        private void RightSpeedTerrain_Click(object sender, EventArgs e)
        {
            RightSpeedTerrain.Checked = !RightSpeedTerrain.Checked;
            if (RightSpeedTerrain.Checked)
            {
                CurrentType |= CTerrainElement.RightSpeedTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.RightSpeedTerrain;
            }
            typelable.Text = CurrentType.ToString();
        }

        private void HurtTerrain_Click(object sender, EventArgs e)
        {
            HurtTerrain.Checked = !HurtTerrain.Checked;
            if (HurtTerrain.Checked)
            {
                CurrentType |= CTerrainElement.HurtTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.HurtTerrain;
            }
            typelable.Text = CurrentType.ToString();
        }

        private void SpringTerrain_Click(object sender, EventArgs e)
        {
            SpringTerrain.Checked = !SpringTerrain.Checked;
            if (SpringTerrain.Checked)
            {
                CurrentType |= CTerrainElement.SpringTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.SpringTerrain;
            }
            typelable.Text = CurrentType.ToString();
        }

        private void inertnessTerrain_Click(object sender, EventArgs e)
        {
            inertnessTerrain.Checked = !inertnessTerrain.Checked;
            if (inertnessTerrain.Checked)
            {
                CurrentType |= CTerrainElement.InertnessTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.InertnessTerrain;
            }
            typelable.Text = CurrentType.ToString();
        }

        private void DeathTerrain_Click(object sender, EventArgs e)
        {
            DeathTerrain.Checked = !DeathTerrain.Checked;
            if (DeathTerrain.Checked)
            {
                CurrentType |= CTerrainElement.DeathTerrain;
            }
            else
            {
                CurrentType ^= CTerrainElement.DeathTerrain;
            }
            typelable.Text = CurrentType.ToString();
        }

        private void Save_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = CONST.XMLFILTER;

            if (elemFileName == "")
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    NewElement.Save(saveFileDialog1.FileName);

                    String mm = System.IO.Path.GetFileName(saveFileDialog1.FileName);
                    NewElement.mFileName = mm;

                    Program.mainWindow.terrainDialog.terrainList.pElements.Add(NewElement);
                }
            }
            else
            {
                NewElement.Save(elemFileName);
                String mm = System.IO.Path.GetFileName(elemFileName);
                NewElement.mFileName = mm;

                Program.mainWindow.terrainDialog.terrainList.pElements.Add(NewElement);
            }
        }

        private void Read_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = CONST.XMLFILTER;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                NewElement = new CTerrainElement();
                if ( NewElement.Read(openFileDialog.FileName) )
                {

                    Bitmap bmpTemp = new Bitmap(NewElement.mBmpName);

                    if (bmpTemp.Width % 16 != 0 || bmpTemp.Height % 16 != 0)
                    {
                        MessageBox.Show(CONST.BMPDIVIDEBY16);
                        return;
                    }

                    if (bmpTemp.Width > 1000 || bmpTemp.Height > 600)
                    {
                        MessageBox.Show(CONST.BMPTOOBIG);
                        return;
                    }


                    int headerHeight = this.Height - this.ClientSize.Height;

                    this.Width = bmpTemp.Width + 20;
                    this.Height = bmpTemp.Height + toolStrip1.Height + headerHeight + 20;

                    if (this.Width < 500)
                    {
                        this.Width = 500;
                    }

                    if (this.Height < 300)
                    {
                        this.Height = 300;
                    }

                    NewElement.bmpTerrainElement = bmpTemp;
                    NewElement.bmpTerrainElement = new Bitmap(NewElement.mBmpName);
                    NewElement.bmpProperty = new Bitmap(NewElement.bmpTerrainElement.Width, NewElement.bmpTerrainElement.Height);

                    Graphics g = Graphics.FromImage(NewElement.bmpProperty);
                    g.Clear(Color.FromArgb(0, 0xFF, 0xFF, 0xFF));
                    Pen pen = new Pen(Color.Black, 1);

                    for (int i = 16; i <= NewElement.bmpProperty.Width; i += 16)
                    {
                        g.DrawLine(pen, i, 0, i, NewElement.bmpProperty.Height);
                    }
                    for (int i = 16; i <= NewElement.bmpProperty.Height; i += 16)
                    {
                        g.DrawLine(pen, 0, i, NewElement.bmpProperty.Width, i);
                    }

                    StartPos.X = (TerrainPic.Width - NewElement.bmpTerrainElement.Width) / 2;
                    StartPos.Y = (TerrainPic.Height - NewElement.bmpTerrainElement.Height) / 2;

                    TerrainPic.Invalidate();
                    elemFileName = openFileDialog.FileName;
                    NewElement.Read(elemFileName);
                }
            }
        }
    }
}
